/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RTO.java
 *
 * Created on Oct 15, 2012, 4:23:16 PM
 */
package gui;

import java.awt.Component;
import java.awt.event.*;

import javax.swing.JOptionPane;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.Duration;

import pms.*;

/**
 *
 * @author moonn52
 */
public class RTO extends javax.swing.JFrame {
	
	private Employee e;

    /** Creates new form RTO 
     * @param e */
    public RTO(Employee e) {
        initComponents();
        this.e = e;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    	this.setTitle("Request Time Off");
    	
    	jcmbStartMonth = new javax.swing.JComboBox();
        jlblStartDate = new javax.swing.JLabel();
        jcmbStartDay = new javax.swing.JComboBox();
        jlblStartMonthDayDiv = new javax.swing.JLabel();
        jlblStartDayYearDiv = new javax.swing.JLabel();
        jtxtStartYear = new javax.swing.JTextField();
        jbtnRequest = new javax.swing.JButton();
        jbtnCancel = new javax.swing.JButton();
        jlblEndDayYearDiv = new javax.swing.JLabel();
        jtxtEndYear = new javax.swing.JTextField();
        jcmbEndMonth = new javax.swing.JComboBox();
        jlblEndMonthDayDiv = new javax.swing.JLabel();
        jcmbEndDay = new javax.swing.JComboBox();
        jlblEndDate = new javax.swing.JLabel();
        jscpDescription = new javax.swing.JScrollPane();
        jtxaDescription = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Request Time Off"); // NOI18N

        DateTime today = new DateTime();
        
        jcmbStartMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        jcmbStartMonth.setSelectedIndex(today.getMonthOfYear()-1);

        jlblStartDate.setText("Start Date");

        jcmbStartDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        
        
        
        jcmbStartDay.setSelectedIndex(today.getDayOfMonth() - 1);

        jlblStartMonthDayDiv.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jlblStartMonthDayDiv.setText("/");

        jlblStartDayYearDiv.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jlblStartDayYearDiv.setText("/");

        jtxtStartYear.setText("" + today.getYear());

        jbtnRequest.setText("Request");
        jbtnRequest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRequestActionPerformed(evt);
            }
        });

        jbtnCancel.setText("Cancel");
        jbtnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelActionPerformed(evt);
            }
        });

        jlblEndDayYearDiv.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jlblEndDayYearDiv.setText("/");

        today = today.plusDays(1);
        
        jtxtEndYear.setText("" + today.getYear());

        jcmbEndMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        jcmbEndMonth.setSelectedIndex(today.getMonthOfYear() - 1);

        jlblEndMonthDayDiv.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
        jlblEndMonthDayDiv.setText("/");

        jcmbEndDay.setMaximumRowCount(0);
        jcmbEndDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        jcmbEndDay.setSelectedIndex(today.getDayOfMonth() - 1);

        jlblEndDate.setText("End Date");

        jtxaDescription.setColumns(20);
        jtxaDescription.setRows(5);
        jscpDescription.setViewportView(jtxaDescription);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jbtnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jbtnRequest, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jlblStartDate)
                            .addComponent(jlblEndDate))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jcmbEndMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(3, 3, 3)
                                .addComponent(jlblEndMonthDayDiv)
                                .addGap(18, 18, 18)
                                .addComponent(jcmbEndDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(3, 3, 3)
                                .addComponent(jlblEndDayYearDiv)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtxtEndYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jcmbStartMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(3, 3, 3)
                                .addComponent(jlblStartMonthDayDiv)
                                .addGap(18, 18, 18)
                                .addComponent(jcmbStartDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(3, 3, 3)
                                .addComponent(jlblStartDayYearDiv)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtxtStartYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jscpDescription, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbStartMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStartDate)
                    .addComponent(jcmbStartDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblStartMonthDayDiv)
                    .addComponent(jlblStartDayYearDiv)
                    .addComponent(jtxtStartYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcmbEndMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcmbEndDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblEndMonthDayDiv)
                    .addComponent(jlblEndDayYearDiv)
                    .addComponent(jtxtEndYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblEndDate))
                .addGap(18, 18, 18)
                .addComponent(jscpDescription)
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnCancel)
                    .addComponent(jbtnRequest))
                .addGap(22, 22, 22))
        );

        pack();
    }// </editor-fold>

    private void jbtnCancelActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	this.dispose();
    }

    private void jbtnRequestActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	int n = JOptionPane.showConfirmDialog(
			    null,
			    "Request Hours Off?",
			    "Are you sure?",
			    JOptionPane.YES_NO_OPTION);
		if (n != 1) {
			// TODO make this put it into the db
			
			DateMidnight start = new DateMidnight(Integer.parseInt(jtxtStartYear.getText()), jcmbStartMonth.getSelectedIndex()+1 , jcmbStartDay.getSelectedIndex() + 1);
			DateMidnight end = new DateMidnight(Integer.parseInt(jtxtEndYear.getText()), jcmbEndMonth.getSelectedIndex() + 1, jcmbEndDay.getSelectedIndex() + 1);
			
			Object frame = null;
			if (start.isBeforeNow() || start.isAfter(end.toInstant())) {
				JOptionPane.showMessageDialog((Component) frame, "You can't have a time traveling vacation.");
			} else if (start.isEqual(end.toInstant())) {
				JOptionPane.showMessageDialog((Component) frame, "Your vacation was null - enjoy it.");
			
			} else {
			try {
				Duration d = new Duration(start, end);
				if (e.getRemainingTimeOff() > d.getStandardDays()) {
					this.e.requestTimeOff(start, end,jtxaDescription.getText());
					this.dispose();
				} else {
					JOptionPane.showMessageDialog((Component) frame, "You don't have that much PTO...");
				}
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			}
			
		}
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RTO.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
            	Employee k = null;
				try {
					k = new Employee("th0r", "THORSMASH");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                new RTO(k).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jbtnCancel;
    private javax.swing.JButton jbtnRequest;
    private javax.swing.JComboBox jcmbEndDay;
    private javax.swing.JComboBox jcmbEndMonth;
    private javax.swing.JComboBox jcmbStartDay;
    private javax.swing.JComboBox jcmbStartMonth;
    private javax.swing.JLabel jlblEndDate;
    private javax.swing.JLabel jlblEndDayYearDiv;
    private javax.swing.JLabel jlblEndMonthDayDiv;
    private javax.swing.JLabel jlblStartDate;
    private javax.swing.JLabel jlblStartDayYearDiv;
    private javax.swing.JLabel jlblStartMonthDayDiv;
    private javax.swing.JScrollPane jscpDescription;
    private javax.swing.JTextArea jtxaDescription;
    private javax.swing.JTextField jtxtEndYear;
    private javax.swing.JTextField jtxtStartYear;
//	private Component frame;
    // End of variables declaration//GEN-END:variables
}

